package com.my.loginbmob.presenter;

import com.my.loginbmob.bean.UserBmobBean;
import com.my.loginbmob.check_util.CheckUser;
import com.my.loginbmob.modle.IUserData;
import com.my.loginbmob.modle.UserDataModle;
import com.my.loginbmob.view.iview.IRegisteView;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * 项目名称：注册逻辑层
 * 创建人：mwb
 * 创建时间：2018/8/2 11:06
 */

public class RegistPresenter implements IRegistePresenter, ICheckUserName {
    private String TAG = "mwbmvp";
    private IRegisteView iRegisteView;
    private IUserData iUserData;
    private CheckUser checkUser;
    private String userName;
    private String password;

    public RegistPresenter(IRegisteView iRegisteView) {
        this.iRegisteView = iRegisteView;
        iUserData = new UserDataModle();
        checkUser = new CheckUser(this);
    }

    @Override
    public void setVisibility(boolean visibility) {

    }

    /**
     * 检查输入
     *
     * @param userName
     * @param password
     * @param passwordAgin
     */
    @Override
    public void checkInput(String userName, String password, String passwordAgin) {
        if (userName.isEmpty()) {  // 用户名是否输入了
//            new Handler().postDelayed(new Runnable() {
//                public void run() {
//                    iRegisteView.setProgressVisibility(0);
//                    iRegisteView.setErroToast("请输入用户名");
//                }
//            }, 2000);
            iRegisteView.setProgressVisibility(0);
            iRegisteView.setErroToast("请输入用户名");

        } else if (userName.length() < 6) { // 用户名是否不小于6位
            iRegisteView.setProgressVisibility(0);
            iRegisteView.setErroToast("请输入6-16位的用户名");
        } else if (password.isEmpty()) { // 密码是否输入了
            iRegisteView.setProgressVisibility(0);
            iRegisteView.setErroToast("请输入密码");
        } else if (password.length() < 6) { // 密码的位数是否不小于6为
            iRegisteView.setProgressVisibility(0);
            iRegisteView.setErroToast("请输入6-16位的密码");
        } else if (passwordAgin.isEmpty()) { // 确认密码是否输入了
            iRegisteView.setProgressVisibility(0);
            iRegisteView.setErroToast("请输入确认密码");
        } else if (!password.equals(passwordAgin)) { // 两次输入的密码是否一致
            iRegisteView.setProgressVisibility(0);
            iRegisteView.setErroToast("两次输入的密码不一致");
        } else { // 是否注册过
            checkUserNameRepeat(userName, password);
        }
    }

    /**
     * 用户名是否重复
     *
     * @param userName
     */
    @Override
    public void checkUserNameRepeat(final String userName, final String password) {
        this.userName = userName;
        this.password = password;
        checkUser.checkUserName(userName);
    }

    @Override
    public void goRegist(final String userName, final String password) {
        iRegisteView.setProgressVisibility(0);
        // 向Bmob中添加数据
        UserBmobBean userBmobBean = new UserBmobBean();
        userBmobBean.setUsername(userName);
        userBmobBean.setPassword(password);
        userBmobBean.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                if (e == null) {
//                    Log.i(TAG, "注册成功:objectId：" + s);
                    iRegisteView.setProgressVisibility(0);
                    iRegisteView.setErroToast("注册成功");
                    iRegisteView.finishActivity();
                } else {
//                    Log.i(TAG, "注册失败：" + e.getMessage());
                    iRegisteView.setProgressVisibility(0);
                    iRegisteView.setErroToast("注册失败:" + e.getMessage());
                }
            }
        });
    }

    @Override
    public void checkUserName(int userNameCount) {
        if (userNameCount == -1) { // 注册查询失败
            iRegisteView.setProgressVisibility(0);
            iRegisteView.setErroToast("注册失败0001"); // 注册查询失败
        } else if (userNameCount > 0) { // 有相同用户名
            iRegisteView.setProgressVisibility(0);
            iRegisteView.setErroToast("账户名已存在");
        } else { // 没有相同用户名
            goRegist(userName, password);
        }
    }
}
